<script lang="ts" setup>
import WBanner from '@/components/w-banner/w-banner.vue'
import { reactive, ref } from "vue";
import { ERelationType } from '@/common/enums/index';
import { IBanner } from '../../types';
import { EventConst, StorageConst } from '@/common/constants';
import { onLoad, onNavigationBarSearchInputClicked } from '@dcloudio/uni-app';
import { useToPage } from '@/common/use/common';

const { navigateTo } = useToPage();
const isuse = uni.getStorageSync(StorageConst.ISUSE);
const banners = ref<IBanner[]>([
  { imageUrl: "../../static/images/delete/banner/1.jpg", relationType: ERelationType.COMMODITY },
  { imageUrl: "../../static/images/delete/banner/2.jpg", relationType: ERelationType.COMMODITY },
  { imageUrl: "../../static/images/delete/banner/3.jpg", relationType: ERelationType.COMMODITY }
]);
const kumgangs = ref([
  { text: "品牌家电", icon: "../../static/images/delete/grid/1.png" },
  { text: "手机专区", icon: "../../static/images/delete/grid/2.png" },
  { text: "新品发售", icon: "../../static/images/delete/grid/3.png" },
  { text: "电脑主机", icon: "../../static/images/delete/grid/4.png" },
  { text: "数码音响", icon: "../../static/images/delete/grid/5.png" }
]);
const state = reactive({
  defaultKeyword: "新款2022年高端智能手机"
});

// 通知程序关闭启动页
const emitCloseSplashscreen = () => {
  // #ifdef APP-PLUS
  // 使用过的用户才会触发通知
  isuse && uni.$emit(EventConst.HOME_RENDERED);
  // #endif
}

// 搜索框点击事件
const onSearchClick = () => {
  navigateTo("/pages/search/search");
}

onLoad(() => {
  console.log("我假装渲染完毕了");
  emitCloseSplashscreen();
});

// 搜索框点击事件
onNavigationBarSearchInputClicked(() => {
  navigateTo("/pages/search/search");
});
</script>

<!-- 首页 -->
<template>
  <div class="container">
    <!-- 头部导航 -->
    <view class="header-nav">
      <view class="statusbar"></view>
      <view class="header-view">
        <view></view>
        <view class="search-view" @click="onSearchClick">
          <uni-easyinput disabled prefixIcon="search" confirmType="search" clearable trim="both" :inputBorder="false"
            :placeholder="state.defaultKeyword">
          </uni-easyinput>
        </view>
        <view class="right-btn">
          <view class="link" @click="onSearchClick">搜索</view>
        </view>
      </view>
    </view>

    <div class="banner-view">
      <w-banner :banners="banners"></w-banner>
    </div>

    <!-- 金刚区 s -->
    <div class="kumgang-view">
      <uni-grid :column="kumgangs.length" :show-border="false">
        <template v-for="grid in kumgangs">
          <uni-grid-item>
            <div class="grid-item">
              <image :src="grid.icon"></image>
              <text>{{ grid.text }}</text>
            </div>
          </uni-grid-item>
        </template>
      </uni-grid>

    </div>

    <!-- 抽奖卡片 -->
    <div class="raffle-view">
      <w-raffle-card></w-raffle-card>
    </div>

    <!-- 热销臻品 -->
    <uni-section class="section" title="热销臻品" type="line" padding>
      <w-commodity-banner-list></w-commodity-banner-list>
    </uni-section>

    <!-- 人气新品 -->
    <uni-section class="section" title="人气新品" type="line" padding>
      <w-commodity-list></w-commodity-list>
    </uni-section>

    <!-- 更多推荐 -->
    <uni-section class="section" title="更多推荐" type="line" padding>
      <w-commodity-card-list></w-commodity-card-list>
    </uni-section>
  </div>
</template>

<style scoped lang="scss">
.container {
  background-color: #fff;
  padding-top: calc($header-height + var(--status-bar-height));

  .banner-view {
    width: 100%;
    height: 300rpx;
    background-color: antiquewhite;
  }
}

.header-nav {
  width: 100%;
  background: $theme-background;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 5;

  .header-view {
    height: $header-height;
    padding: 0 30rpx;
    display: flex;
    align-items: center;

    .search-view {
      flex: 1;
      background-color: #fff;
      border-radius: 999px;
      margin: 0 30rpx;
      overflow: hidden;

      :deep(.uni-easyinput) {
        height: 60rpx;
        border-radius: 999px;
      }
    }

    .right-btn {
      color: #fff;
      font-size: 28rpx;
    }
  }
}

.raffle-view,
.kumgang-view {
  padding: $padding;
}

.kumgang-view {
  .grid-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;

    image {
      width: 96rpx;
      height: 96rpx;
    }

    text {
      font-size: 24rpx;
    }
  }
}

:deep(.section) {
  .uni-section-header {
    background-color: #fff;
    box-shadow: 0px 0px 24px 0px rgb(255 106 0 / 40%) inset;

    .uni-section-header__content {

      .uni-section__content-title,
      span {
        font-size: 32rpx !important;
      }
    }
  }

  .uni-section-content {
    padding: 0 !important;
  }

  .uni-section-header__decoration {
    background-color: $primary-color;
  }
}
</style>
